/**
 * Created by 白雨浓 on 17-9-21 下午3:02.
 *
 * 时间日期工具
 */
const DateUtil = {
  install(Vue) {
    Vue.prototype.$date = {
      toDate(datetime) {
        if (datetime) {
          datetime = datetime.trim().split(" ");
          const date = datetime[0].split('-');

          if (!datetime[1]) {
            return new Date(date[0], date[1] - 1, date[2]);
          }

          const time = datetime[1].split(':');
          return new Date(date[0], date[1] - 1, date[2], time[0], time[1], time[2]);
        }
      },
      format(date, fmt) {
        if (typeof date === 'string')
          date = new Date(date);

        if (/(y+)/.test(fmt)) {
          fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
        }
        let o = {
          'M+': date.getMonth() + 1,
          'd+': date.getDate(),
          'H+': date.getHours(),
          'm+': date.getMinutes(),
          's+': date.getSeconds()
        };
        for (let k in o) {
          if (new RegExp(`(${k})`).test(fmt)) {
            let str = o[k] + '';
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : ('00' + str).substr(str.length));
          }
        }
        return fmt;
      },
      stampToDate() {

      },
      dateToStamp(datetime) {
        if (typeof datetime === 'string') {
          datetime = this.toDate(datetime);
        }
        return datetime.getTime();
      }
    }
  }
};

export default DateUtil;
